/* ========================================================================
 * PROJECT: FTIR Blob Tracker
 * ========================================================================
 * This work is based on the original FTIR Blob Tracker developed by
 *   Woonhyuk Baek
 *   Woontack Woo
 *   U-VR Lab, GIST of Gwangju in Korea.
 * 
 *   http://uvr.gist.ac.kr/
 *
 * Copyright of the derived and new portions of this work
 *     (C) 2009 GIST U-VR Lab.
 *
 * This framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For further information please contact 
 *   Woonhyuk Baek
 *   <windage@live.com>
 *   GIST U-VR Lab.
 *   Department of Information and Communication
 *   Gwangju Institute of Science and Technology
 *   1, Oryong-dong, Buk-gu, Gwangju
 *   South Korea
 * ========================================================================
 ** @author   Woonhyuk Baek
 * ======================================================================== */

#include "SelfCalibrator.h"
using namespace windage;

#include "Homography.h"

void SelfCalibrator::SetGridSize(int GRID_X, int GRID_Y)
{
	this->gridX = GRID_X;
	this->gridY = GRID_Y;
	this->pointCount = (GRID_X-1) * (GRID_Y-1);

	this->adjustmentPoints.resize((GRID_X) * (GRID_Y));
	this->staticPoints.resize((GRID_X) * (GRID_Y));
	this->homographys.resize(this->pointCount);

	// initialize
	double dx = 1.0 / (double)(this->gridX-1);
	double dy = 1.0 / (double)(this->gridY-1);
	for(int y=0; y<this->gridY; y++)
	{
		for(int x=0; x<this->gridX; x++)
		{
			adjustmentPoints[y*this->gridX + x] = windage::Vector2(dx * (x), dy * (y));
			staticPoints[y*this->gridX + x] = windage::Vector2(dx * (x), dy * (y));
		}
	}

	for(int i=0; i<this->pointCount; i++)
	{
		for(int y=0; y<3; y++)
		{
			for(int x=0; x<3; x++)
			{
				if(x == y)
					this->homographys[i].m[y][x] = 1.0;
				else
					this->homographys[i].m[y][x] = 0.0;
			}
		}
	}
}

bool SelfCalibrator::UpdateHomography()
{
	std::vector<windage::Vector2> pt1; pt1.resize(4);
	std::vector<windage::Vector2> pt2; pt2.resize(4);

	windage::Homography calculator(1, 1);
	calculator.AttatchImagePoints(&pt1);
	calculator.AttatchObjectPoints(&pt2);
	calculator.ResetFlip();

	int count = 0;
	for(int y=0; y<this->gridY-1; y++)
	{
		for(int x=0; x<this->gridX-1; x++)
		{
			pt1[0] = this->adjustmentPoints[(y+0)*this->gridX + (x+0)];
			pt1[1] = this->adjustmentPoints[(y+0)*this->gridX + (x+1)];
			pt1[2] = this->adjustmentPoints[(y+1)*this->gridX + (x+1)];
			pt1[3] = this->adjustmentPoints[(y+1)*this->gridX + (x+0)];

			pt2[0] = this->staticPoints[(y+0)*this->gridX + (x+0)];
			pt2[1] = this->staticPoints[(y+0)*this->gridX + (x+1)];
			pt2[2] = this->staticPoints[(y+1)*this->gridX + (x+1)];
			pt2[3] = this->staticPoints[(y+1)*this->gridX + (x+0)];

			calculator.Do();
			this->homographys[count] = calculator.GetHomography();
			count++;
		}
	}
	return true;
}

windage::Vector2 SelfCalibrator::ConvertPoint(windage::Vector2 point)
{
	int indexX = -1;
	int indexY = -1;
//*
	for(int y=0; y<this->gridY-1; y++)
	{
		for(int x=0; x<this->gridX-1; x++)
		{
			windage::Vector2 pt1, pt2, d1, d2;
			pt1 = this->adjustmentPoints[(y+0)*this->gridX + (x+0)];
			pt2 = this->adjustmentPoints[(y+1)*this->gridX + (x+0)];
			d1 = pt2 - pt1;
			d2 = point - pt1;
			double z1 = (d1.x*d2.y - d1.y*d2.x);

			pt1 = this->adjustmentPoints[(y+1)*this->gridX + (x+0)];
			pt2 = this->adjustmentPoints[(y+1)*this->gridX + (x+1)];
			d1 = pt2 - pt1;
			d2 = point - pt1;
			double z2 = (d1.x*d2.y - d1.y*d2.x);

			pt1 = this->adjustmentPoints[(y+1)*this->gridX + (x+1)];
			pt2 = this->adjustmentPoints[(y+0)*this->gridX + (x+1)];
			d1 = pt2 - pt1;
			d2 = point - pt1;
			double z3 = (d1.x*d2.y - d1.y*d2.x);

			pt1 = this->adjustmentPoints[(y+0)*this->gridX + (x+1)];
			pt2 = this->adjustmentPoints[(y+0)*this->gridX + (x+0)];
			d1 = pt2 - pt1;
			d2 = point - pt1;
			double z4 = (d1.x*d2.y - d1.y*d2.x);

			// is inside
			if(z1 <= 0 && z2 <= 0 && z3 <= 0 && z4 <= 0)
			{
				indexX = x;
				indexY = y;
			}
		}
	}
//*/

/*
	double indexXtemp = point.x * (double)(this->gridX-1);
	double indexYtemp = point.y * (double)(this->gridY-1);
	indexX = (int)indexXtemp;
	indexY = (int)indexYtemp;
//*/
	if( 0 > indexX || indexX >= this->gridX-1 ||
		0 > indexY || indexY >= this->gridY-1)
		return windage::Vector2(-1, -1);

	windage::Vector3 tempPoint(point.x, point.y, 1.0);
	windage::Vector3 tempResult;
	tempResult = this->homographys[indexY*(this->gridX-1) + indexX] * tempPoint;
	tempResult /= tempResult.z;

	windage::Vector2 result(tempResult.x, tempResult.y);
	return result;
}